package com.atguigu1.conection;


import org.junit.Test;

import java.io.InputStream;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.util.Properties;

public class ConnectionTest {
    @Test
    public void testConnction1() throws Exception {

        Driver driver = new com.mysql.jdbc.Driver();

        //jdbc:mysql:协议
        //localhost:ip协议
        //3306:默协议端口号
        //test:test数据库
        String ur1 = "jdbc:mysql://localhost:3306/test" +
                "?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone = GMT";

        //将用户名和密码封装在Properties中
        Properties info = new Properties();
        info.setProperty("user", "root");
        info.setProperty("password", "monster021109");

        Connection conn = driver.connect(ur1, info);
        System.out.println(conn);

    }

    @Test
    public void testConnnection2() throws Exception {
        //获取Driver实现类对象，使用反射
        Class clazz = Class.forName("com.mysql.jdbc.Driver");
        Driver driver = (Driver) clazz.newInstance();

        //2.提供要连接的数据库
        String ur1 = "jdbc:mysql://localhost:3306/test" +
                "?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone = GMT";

        //3.连接需要的用户名和密码
        Properties info = new Properties();
        info.setProperty("user", "root");
        info.setProperty("password", "monster021109");

        //4.获取连接
        Connection conn = driver.connect(ur1, info);
        System.out.println(conn);
    }

    // 方式三：使用DriverManager替换Driver
    @Test
    public void testConnction3() throws Exception {
        // 1.获取Driver实现类对象
        Class clazz = Class.forName("com.mysql.jdbc.Driver");
        Driver driver = (Driver) clazz.newInstance();

        // 2.提供另外三个连接的基本信息：
        String ur1 = "jdbc:mysql://localhost:3306/test" +
                "?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone = GMT";
        String user = "root";
        String password = "monster021109";

        // 注册驱动
        DriverManager.registerDriver(driver);

        // 获取连接
        Connection conn = DriverManager.getConnection(ur1, user, password);
        System.out.println(conn);
    }

    // 方式四：可以只是加载驱动，不用显示的注册驱动了
    @Test
    public void testConnction4() throws Exception {
        // 1.提供另外三个连接的基本信息：
        String ur1 = "jdbc:mysql://localhost:3306/test" +
                "?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone = GMT";
        String user = "root";
        String password = "monster021109";

        // 2.加载Driver
        Class.forName("com.mysql.jdbc.Driver");
//		Driver driver = (Driver) clazz.newInstance();
//		// 注册驱动
//		DriverManager.registerDriver(driver);

        // 3.获取连接
        Connection conn = DriverManager.getConnection(ur1, user, password);
        System.out.println(conn);
    }

    //方式五：将数据库连接需要的四个基本信息声明在配置文件中，通过获取配置文件来获取连接
    @Test
    public void getConnection5() throws Exception {
        //1.读取配置文件
        InputStream is = ConnectionTest.class.getClassLoader().getResourceAsStream("jdbc.properties");

        Properties pros = new Properties();

        pros.load(is);

        //2.获取信息
        String user = pros.getProperty("user");
        String password = pros.getProperty("password");
        String url = pros.getProperty("url");
        String driverClass = pros.getProperty("driverClass");

        //3.加载驱动
        Class.forName(driverClass);

        Connection conn = DriverManager.getConnection(url, user, password);
        System.out.println(conn);
    }
}



